package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.mysql.utils.PageHelperUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务表 服务实现类
 * </p>
 *
 * @author Gmx
 * @since 2024-12-11
 */

@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {
    //@Resource
    //private ServeMapper serveMapper;

    @Resource
    private ServeItemMapper serveItemMapper;

    @Resource
    private RegionMapper regionMapper;
    @Autowired
    private ServeMapper serveMapper;

    @Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO dto) {
        return PageHelperUtils.selectPage(dto, () -> baseMapper.selectServeListByRegionId(dto.getRegionId()));
    }



    /*@Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO dto) {
        return PageHelperUtils.selectPage(dto, new PageHelperUtils.QueryExecutor<ServeResDTO>() {
            @Override
            public List<ServeResDTO> query() {
                return baseMapper.selectServeListByRegionId(dto.getRegionId());
            }
        });
    }*/

    /*@Override
    public PageResult<ServeResDTO> page(ServePageQueryReqDTO dto) {
        PageHelper.startPage(dto.getPageNo().intValue(),dto.getPageSize().intValue());
        List<ServeResDTO> list = baseMapper.selectServeListByRegionId(dto.getRegionId());
        Page<ServeResDTO> page = (Page<ServeResDTO>) list;
        return new PageResult<>((long)page.getPages(),page.getTotal(),list);
    }*/

    @Override
    public void batchAdd(List<ServeUpsertReqDTO> dtos) {
        //1. 如果任意服务项未启动，不能添加区域服务
        for (ServeUpsertReqDTO dto : dtos) {
            ServeItem serveItem = serveItemMapper.selectById(dto.getServeItemId());
            if (serveItem == null || serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
                throw new ForbiddenOperationException("服务未启动");
            }
            //2. 如果任意服务项已经在区域服务中，不能重复添加
            Integer count = lambdaQuery().eq(Serve::getServeItemId, dto.getServeItemId())
                    .eq(Serve::getRegionId, dto.getRegionId())
                    .count();
            if (count > 0) {
                throw new ForbiddenOperationException("服务项已经在区域服务中");
            }
            //3. 封装实体类对象，添加到数据库
            Serve serve = BeanUtil.toBean(dto, Serve.class);
            Region region = regionMapper.selectById(dto.getRegionId());
            serve.setCityCode(region.getCityCode());
            save(serve);
        }
    }

    @Override
    public void updatePrice(Long id, BigDecimal price) {
        boolean updated = lambdaUpdate().eq(Serve::getId, id)
                .set(Serve::getPrice, price)
                .update();

        if (!updated) {
            throw new CommonException("修改服务价格失败");
        }
    }

    @Override
    public void onSale(Long id) {
        //查询区域服务是否存在
        Serve serve = getById(id);
        if (null ==serve) {
            throw new ForbiddenOperationException("区域服务不存在");
        }
        //如果区域服务不是草稿或下架状态, 则不允许上架
        if (FoundationStatusEnum.INIT.getStatus() != serve.getSaleStatus()
        || FoundationStatusEnum.DISABLE.getStatus() == serve.getSaleStatus()) {
            throw new ForbiddenOperationException("草稿或者下架状态才允许上架");
        }
        //判断服务项是否启用
        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        if (null == serveItem) {
            throw new ForbiddenOperationException("服务项不存在");
        }

        if (serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("服务项未启用,不允许上架");
        }
        //改区域服务状态为已上架
        boolean updated = lambdaUpdate().eq(Serve::getId, id)
                .set(Serve::getSaleStatus, FoundationStatusEnum.ENABLE.getStatus())
                .update();
        if (!updated) {
            throw new ForbiddenOperationException("服务上架失败");
        }

    }


}
